
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Blueprint, request
from flask_jwt_extended import create_access_token, get_jwt_identity, jwt_required
import lj_action.action_data

from lj_api import tool_flask

api_auth = Blueprint('auth', __name__)


def account_orm():
    return lj_action.action_data.init_action_data('account')


@api_auth.route("/login", methods=["GET", "POST"])
def login():
    username = tool_flask.get_param("username")
    password = tool_flask.get_param("password")

    accounts = account_orm().listFilter(account_orm().entity.account ==
                                        username, account_orm().entity.password == password)

    if len(accounts) > 0:
        return create_access_token(identity=username)
    else:
        return '账号密码错误,登录失败...', 500,


@api_auth.route("/reg", methods=["GET", "POST"])
def reg():
    return account_orm().sync(tool_flask.get_merge_param())


@api_auth.route("/verify", methods=["GET", "POST"])
@jwt_required()
def verify():
    current_user = get_jwt_identity()
    return f'ok {current_user}'
